<template>
  <!-- 添加审核人弹出框 -->
  <el-dialog
    :visible.sync="Visible"
    width="30%"
    append-to-body
    title="查询子规则"
    :before-close="close"
  >
    <template>
      <el-form :model="dataForm" :rules="rules" ref="dataForm">
        <el-form-item label="频率设置:" prop="once">
          <el-radio-group v-model="dataForm.once">
            <el-radio-button v-if="checkedValue.cap" :label="2">每天</el-radio-button>
            <el-radio-button v-if="checkedValue.limitless" :label="3">无限</el-radio-button>
            <el-radio-button v-if="checkedValue.one" :label="1">一次</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="限制次数:" v-if="dataForm.once == 2 && checkedValue.cap" prop="count">
          <el-radio-group v-model="dataForm.count">
            <el-tooltip class="item" effect="dark" content="注：每篇文章/课程获取上限次数" placement="top-start">
                  <el-input-number controls-position="right" :min="1" v-model="dataForm.count" style="width: 200px"/>
            </el-tooltip>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设置积分:" prop="grade">
          <el-tooltip class="item" effect="dark" content="注：设置积分不能大于上限积分" placement="top-start">
              <el-input-number controls-position="right" :min="1" style="width: 200px" @input="change($event)"
                            v-model="dataForm.grade"/>
          </el-tooltip>
        </el-form-item>
        <el-form-item label="是否开启:" prop="isEnable">
          <el-switch
            v-model="dataForm.isEnable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
    </template>
    <!--    <set-up ref="setUp"></set-up>-->
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="onsubmit()" :loading="loading">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        loading: false,
        Visible: false,
        maxCount: '',
        dataForm: {
          once: '',
          cCode: '',
          isEnable: '',
          grade: '',
          id: '',
          count: 1,
          val: ''
        },
        checkedValue: {
          cap: false,
          limitless: false,
          one: false
        },
        tableData: [],
        parentRuleId: '',
        rules: {
          grade: [
            {required: true, message: '积分不能为空', trigger: 'blur'}
          ],
          count: [
            {required: true, message: '次数不能为空', trigger: 'blur'}
          ],
          isEnable: [
            {required: true, message: '是否开启', trigger: 'blur'}
          ],
          once: [
            { required: true, message: '请选择频率', trigger: 'change' }
          ]
        }
      }
    },
    methods: {
      init(row, maxCount, parentRuleId) {
        this.parentRuleId = parentRuleId
        this.maxCount = maxCount
        this.dataForm = {
          once: '',
          cCode: '',
          isEnable: '',
          grade: '',
          id: '',
          count: 1,
          brief: '',
          name: '',
          checkedValue: {
            cap: false,
            limitless: false,
            one: false
          },
          val: ''
        }
        this.checkedValue = JSON.parse(row.checkedValue)
        this.dataForm.once = this.checkedValue.cap ? this.dataForm.once = 2 : this.checkedValue.one ? this.dataForm.once = 1 : this.dataForm.once = 3
          // if (this.checkedValue.one) {
        //   this.dataForm.once = '1'
        // } else if (this.checkedValue.cap) {
        //   this.dataForm.once = '2'
        // }
        this.dataForm.id = row.id
        this.dataForm.cCode = row.cCode
        this.Visible = true
        this.loading = false
        if (row.count) {
          this.getdetail()
        }
      },
      change() {
        this.$forceUpdate()
      },
      onsubmit() {
        // eslint-disable-next-line eqeqeq
        if (this.maxCount != -1) {
          if (this.dataForm.grade * this.dataForm.count > this.maxCount) {
            this.$notify.error({title: '失败', message: '最大值不能超过父规则上限值！'})
            return false
          }
        }
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/integral-admin/integral/rule/updateChildRule'),
              method: 'post',
              data: {
                markId: this.dataForm.id,
                ruleId: this.parentRuleId,
                isEnable: this.dataForm.isEnable ? 1 : 0,
                cCode: this.dataForm.cCode,
                once: this.dataForm.once ? this.dataForm.once : 2,
                count: this.dataForm.once === 1 ? 1 : this.dataForm.once === 2 ? this.dataForm.count : -1,
                grade: this.dataForm.grade
              }
            }).then(res => {
              this.Visible = false
              this.$emit('refresh')
              // this.tableData = res.data
            })
          }
        })
      },
      getdetail() {
        this.$http({
          url: this.$http.adornUrl('/integral-admin/integral/rule/getRuleChildInfo'),
          method: 'post',
          data: {
            cCode: this.dataForm.cCode
          }
        }).then(res => {
          if (res.data !== null) {
            this.dataForm = res.data
            this.dataForm.count = res.data.count > -1 ? res.data.count : ' '
          } else {
            this.dataForm.isEnable = 0
            this.dataForm.grade = 1
            this.dataForm.once = 2
            this.dataForm.count = 2
          }
          // this.Visible = false
          // this.$emit('refresh')
          // this.tableData = res.data
        })
      },
      close() {
        this.Visible = false
      }
    }
  }
</script>

<style scoped>
</style>
